package ru.usu.gv.utils.geometry;

import javax.vecmath.Point2d;
import javax.vecmath.Point3d;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author spupyrev
 * 28.10.2009
 */
public class Polyline
{
	private List<Point2d> points;

	public Polyline()
	{
		points = new ArrayList<Point2d>();
	}

	public void addPoint(Point2d point)
	{
		points.add(point);
	}

	public void addPoint(double x, double y)
	{
		addPoint(new Point2d(x, y));
	}

	public void addPoint(Point3d point)
	{
		addPoint(point.x, point.y);
	}

	public void reverse()
	{
		Collections.reverse(points);
	}

	public List<Point2d> getPoints()
	{
		return points;
	}

	public Point3d[] toControlPoints()
	{
		Point3d[] res = new Point3d[points.size()];
		for (int i = 0; i < points.size(); i++)
			res[i] = new Point3d(points.get(i).x, points.get(i).y, 0);

		return res;
	}

	public boolean isCW()
	{
		if (points.size() < 3)
			return true;

		Point2d p0 = points.get(0);
		Point2d p1 = points.get(1);
		Point2d p2 = points.get(2);

		double s = (p0.x - p1.x) * (p0.y + p0.y) + (p1.x - p2.x) * (p1.y + p2.y) + (p2.x - p0.x) * (p2.y + p0.y);
		return (s < 0.0);
	}
}
